package com.yxq.mdt.app.modules.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.modules.login.model.LoginModel;
import com.yxq.mdt.app.modules.login.presenter.ILoginContract;
import com.yxq.mdt.app.modules.login.presenter.LoginPresenter;
import com.yxq.mdt.app.modules.main.ui.MainActivity;
import com.yxq.mdt.app.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 绑定手机号
 */
public class BindPhoneActivity extends BaseActivity<LoginPresenter> implements ILoginContract.View {

    UserBean userBean;
    @BindView(R.id.login_et_account)
    EditText loginEtAccount;
    @BindView(R.id.login_et_pwd)
    EditText loginEtPwd;
    CountDownTimer countDownTimer;
    @BindView(R.id.login_et_re_pwd)
    EditText loginEtRePwd;
    @BindView(R.id.login_et_code)
    EditText loginEtCode;
    @BindView(R.id.login_tv_code)
    TextView loginTvCode;
    Handler handler = new Handler();

    @Override
    public int getLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public void initView() {
        mPresenter = new LoginPresenter(this, new LoginModel());
        userBean = JSON.parseObject(getIntent().getStringExtra("data"), UserBean.class);
        if (null != mCommonTitleBar && userBean != null) {
            mCommonTitleBar.setListener((v, action, extra) -> {

                if (action == CommonTitleBar.ACTION_LEFT_BUTTON) {
                    if (getIntent().getBooleanExtra("finish", false)) {
                        finish();
                        return;
                    }
                    startActivity(new Intent(mActivity, LoginActivity.class));
                    finish();
                }
            });
        }
        loginEtRePwd.setText("lp123456");
        loginEtPwd.setText("lp123456");
    }

    @Override
    public void onBackPressed() {
        if (userBean != null) {
            if (getIntent().getBooleanExtra("finish", false)) {
                finish();
                return;
            }
            startActivity(new Intent(mActivity, LoginActivity.class));
            finish();
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @OnClick({R.id.login_btn_login, R.id.login_tv_code})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn_login://绑定手机号
                bindPhone();
                break;
            case R.id.login_tv_code://验证码
                verifyCode();
                break;
        }
    }

    /**
     * 绑定手机号
     */
    private void bindPhone() {
        if (loginEtAccount.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入手机号码");
            return;
        }

        if (!StringUtils.isMobileExact(loginEtAccount.getText().toString())) {
            ToastUtils.showShort("请输入合法的手机号码");
            return;
        }
        if (loginEtPwd.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (loginEtRePwd.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入确认密码");
            return;
        }
        if (loginEtCode.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入验证码");
            return;
        }
        if (!loginEtRePwd.getText().toString().equals(loginEtPwd.getText().toString())) {
            ToastUtils.showShort("两次输入的密码不一致");
            return;
        }
        showLoadingDialog();
        mPresenter.bindPhone(loginEtAccount.getText().toString(), loginEtPwd.getText().toString(), userBean.getAccess_token(), loginEtCode.getText().toString());
    }

    /**
     * 发送验证码
     */
    private void verifyCode() {
        if (loginEtAccount.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入手机号码");
            return;
        }
        if (!StringUtils.isMobileExact(loginEtAccount.getText().toString())) {
            ToastUtils.showShort("请输入合法的手机号码");
            return;
        }
        showLoadingDialog();
        mPresenter.verifyCode(loginEtAccount.getText().toString(), 1);
    }

    @Override
    public void onLoginSuccess(UserBean userBean) {

    }

    @Override
    public void onLoginFailed(String error) {

    }

    @Override
    public void onVerifyCodeSuccess(String info) {
        hideLoadingDialog();
        ToastUtils.showShort("验证码已发送到你手机");
        timer();
    }

    private void stopTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    private void timer() {
        stopTimer();
        loginTvCode.setEnabled(false);
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                loginTvCode.setText((l / 1000) + "s");
            }

            @Override
            public void onFinish() {
                loginTvCode.setText("获取验证码");
                loginTvCode.setEnabled(true);
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    @Override
    public void onVerifyCodeFailed(String error) {
        hideLoadingDialog();
        ToastUtils.showShort(error);
    }

    @Override
    public void onRegisterSuccess(String userBean) {

    }

    @Override
    public void onRegisterFailed(String error) {

    }

    /**
     * 绑定手机号成功
     *
     * @param result
     */
    @Override
    public void onBindPhoneSuccess(String result) {
        hideLoadingDialog();
        userBean.setBinding(loginEtAccount.getText().toString());
        //保存登录信息
        Hawk.delete(SPKey.SP_KEY_USER_MY_INFO);
        Hawk.delete(SPKey.SP_KEY_USER_INFO);
        Hawk.put(SPKey.SP_KEY_USER_INFO, userBean);
        handler.postDelayed(() -> {
            if (getIntent().getBooleanExtra("finish", false)) {
                finish();
                return;
            }
            //跳转主页
            Intent intent = new Intent(BindPhoneActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }, 50);
    }

    @Override
    public void onBindPhoneFailed(String error) {
        hideLoadingDialog();
        ToastUtils.showShort(error);
    }
}
